<?php
class Acontribute {
	public static function main($funcode) {
		sview::verify(true, true);
		switch ($funcode) {
			case 1:
				self::__dosupports();
				break;
			case 2:
				self::__dovipsquard();
				break;
			case 3:
				self::__doassistant();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//多维度数组排序
	private static function _multi_sort($list) {
		foreach ($list as $item) {
			$coins[] = $item['coin'];
			$lvs[]   = $item['rlv'];
		}
		array_multisort($coins, SORT_DESC, $lvs, SORT_DESC, $list);
		return $list;
	}
	
	//会员贡献榜3-8-1
	private static function __dosupports() {
		$acl    = acl::getinst('vedio');
		$type   = isset(sview::$ctrl['type'])? intval(sview::$ctrl['type']):-1;
		$list   = array();
		$userid = $acl->user['id'];
		$where  = array('anchorid'=>$userid);
		if ($type == 1) {
			$where[] = array('symd', date('Ymd', config::$nowtime-86400*30), orm::OP_GE);
		} else if ($type == 0) {
			$where[] = array('symd', date('Ymd', config::$nowtime-86400*7), orm::OP_GE);
		}
		$data   = model::load('live/fanspay')->set_devision($userid)->index_list(0, 10, $where);
		if ($data) {
			$uorm   = model::load('live/userinfo');
			$where  = array('userid'=>$userid);
			$studio = model::load('live/studio')->get_item($where);
			$guard  = model::load('live/guard')->get_one(api_guard::MRefType);
			$vips   = api_guard::uids($studio['id']);
			foreach ($data as $item) {
				$user   = $uorm->set_devision($item['userid'])->get_one($item['userid']);
				$gicon  = (!!$vips && in_array($item['userid'], $vips))? $guard['iconsrc']:'';
				$list[] = array('userid'=>$item['userid'], 'nickname'=>$user['nickname'], 'rlv'=>$user['lv'], 
					'gicon'=>$gicon, 'avtar'=>$user['thumb'], 'coin'=>$item['coin']);
			}
			$list = self::_multi_sort($list);
		}
		sview::$view->assign_by_ref('datasets', $list);
		sview::$view->display();
	}
	
	//VIP用户贡献榜3-8-2
	private static function __dovipsquard() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$where  = array('userid'=>$userid);
		$studio = model::load('live/studio')->get_item($where);
		$data   = array('list'=>array(), 'total'=>0, 'limit'=>api_guard::YMAXSIZE);
		$users  = api_guard::user($studio['id']);
		if ($users) {
			$uorm = model::load('live/userinfo');
			$form = model::load('live/fanspay')->set_devision($userid);
			$guard= model::load('live/guard')->get_one(api_guard::MRefType);
			foreach ($users as &$item) {
				$user   = $uorm->set_devision($item['userid'])->get_one($item['userid']);
				$where  = array('anchorid'=>$userid, 'userid'=>$item['userid']);
				$fans   = $form->get_item($where, 'userid, sum(coin) as coin');
				$list[] = array('userid'=>$item['userid'], 'nickname'=>$user['nickname'], 'rlv'=>$user['lv'], 
					'gicon'=>$guard['iconsrc'], 'avtar'=>$user['thumb'], 'coin'=>$fans? $fans['coin']:0);
			}
			$data['list']  = self::_multi_sort($list);
			$data['total'] = count($users);
		}
		sview::$view->assign_by_ref('datasets', $data);
		sview::$view->display();
	}
	
	//房管贡献榜3-8-3
	private static function __doassistant() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$where  = array('userid'=>$userid);
		$studio = model::load('live/studio')->get_item($where);
		$where  = array('studioid'=>$studio['id']);
		$sorm   = model::load('live/stumanager')->set_devision($studio['id']);
		$users  = $sorm->get_only_list(0, -1, $where, 'userid');
		$data   = array('list'=>array(), 'total'=>0, 'limit'=>api_studio::MAXASSISTANS);
		if ($users) {
			$uorm  = model::load('live/userinfo');
			$form  = model::load('live/fanspay')->set_devision($userid);
			$guard = model::load('live/guard')->get_one(api_guard::MRefType);
			$vips  = api_guard::uids($studio['id']);
			foreach ($users as &$item) {
				$user   = $uorm->set_devision($item['userid'])->get_one($item['userid']);
				$where  = array('anchorid'=>$userid, 'userid'=>$item['userid']);
				$fans   = $form->get_item($where, 'userid, sum(coin) as coin');
				$gicon  = (!!$vips && in_array($item['userid'], $vips))? $guard['iconsrc']:'';
				$list[] = array('userid'=>$item['userid'], 'nickname'=>$user['nickname'], 'rlv'=>$user['lv'], 
					'gicon'=>$gicon, 'avtar'=>$user['thumb'], 'coin'=>$fans? $fans['coin']:0);
			}
			$data['list']  = self::_multi_sort($list);
			$data['total'] = count($users);
		}
		sview::$view->assign_by_ref('datasets', $data);
		sview::$view->display();
	}
}